package com.coffee.service.impl;

import com.coffee.common.CustomException;
import com.coffee.dao.UserDAO;
import com.coffee.entity.User;
import com.coffee.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDAO userDAO;
    @Override
    public User login(User userParam) {
        return userDAO.login(userParam);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void register(User userParam) {
        int count = userDAO.selectPhone(userParam.getPhone());
        if (count != 0) {
            throw new CustomException("手机号已被注册");
        }
        userParam.setId(UUID.randomUUID().toString());
        userParam.setStatus(1);
        userParam.setPassword(DigestUtils.md5DigestAsHex(userParam.getPassword().getBytes()));
        userDAO.register(userParam);
    }

    @Override
    public User getById(String userId) {
        return userDAO.getById(userId);
    }

    @Override
    public List<User> list() {
        return userDAO.list();
    }

    @Override
    public void status(User user) {
        userDAO.status(user);
    }
}
